<template>
  <div class="publicBox" id="shoperNewbuildCss">

<el-form ref="form" :model="ruleForm" :rules="rules"  class="demo-form-inline publicEditFrom" label-width="160px" label-position="left">

<el-row>
     <div class="pubPageTagP">
       <p v-if="!disabledFlag">区域管理/新增</p>
               <p v-else>区域管理/编辑</p>
     </div>
</el-row>

<el-row>
         <el-form-item label="区域编码" prop="no">
                 <el-input class="pubSerIpt" v-if="!disabledFlag" v-model="ruleForm.no"  placeholder="请输入区域编码，三位字母缩写"></el-input>
                  <p v-else>{{ruleForm.no}}</p>
       </el-form-item>
</el-row>


<el-row>
         <el-form-item label="区域名称" prop="name">
                 <el-input class="pubSerIpt" v-if="!disabledFlag" v-model="ruleForm.name"  placeholder="请输区域名称"></el-input>
                <p v-else>{{ruleForm.name}}</p>
       </el-form-item>
</el-row>

<el-row>
        <el-form-item label="区域城市" prop="cityid" class="label_right" >
        <el-select v-model="ruleForm.cityid" v-if="!disabledFlag" filterable  placeholder="深圳市" class="pubSlt200">
            <el-option
            v-for="item in getselect"
            :key="item.id"
            :label="item.name"
            :value="item.id" 
            >
            </el-option>
        </el-select>
        <p v-else>{{ruleForm.cityid}}</p>
        </el-form-item>
</el-row>

<el-row>
         <el-form-item label="详细地址" prop="address">
                 <el-input class="pubSerIpt" v-if="!disabledFlag" v-model="ruleForm.address"  placeholder="请输入区域详细地址"></el-input>
                <p v-else>{{ruleForm.address}}</p>
       </el-form-item>
       
</el-row>

<el-row>
       <el-form-item label='覆盖范围' prop='coveragearea'>
         <el-input class="pubSerIpt"  v-if="!disabledFlag" v-model="ruleForm.coveragearea"  placeholder="请输入区域覆盖范围，多项请用逗号“，”隔开"></el-input>
          <p v-else>{{ruleForm.coveragearea}}</p>
       </el-form-item>
</el-row>

<el-row>
         <el-form-item label="负责人" prop="principal">
                 <el-input class="pubSerIpt" v-model="ruleForm.principal"  placeholder="请输入该区域负责人"></el-input>
       </el-form-item>
</el-row>

<el-row>
         <el-form-item label="联系电话" prop="phone">
                 <el-input class="pubSerIpt" v-model="ruleForm.phone"  placeholder="请输入负责人联系电话"></el-input>
       </el-form-item>
</el-row>
<el-row class="pubSubmitBtn-row">
           <el-form-item>
                         <el-button class="pubBtnBlue" type="primary" @click="submitForm">提交</el-button>
                         <el-button class="pubBtnGray" @click="canCleFn"  type="primary">取消</el-button>
       </el-form-item>
</el-row>


</el-form>
    
  </div>
</template>

<script>
import * as Api from "@api/shop.js"
export default {
  components: {


  },
  data() {
    return {
      disabledFlag:false,
      getselect:[],
      rowData:"",
     ruleForm:{
      principal:"",
      phone:"",
      no:"",
      name:"",
      cityid:"",
      address:"",
      coveragearea:""
     },
      rules: {
          no: [
            {required: true, message: '请输入区域编码', trigger: 'blur' }
          ],
          name: [
            {required: true, message: '请输入区域名称', trigger: 'blur' }
          ],
          cityid: [
            {required: true, message: '请选择区域城市', trigger: 'blur' }
          ],
          address: [
            {required: true, message: '请输入区域详细地址', trigger: 'blur' }
          ],
          coveragearea: [
            {required: true, message: '请输入区域覆盖范围，多项请用逗号“，”隔开', trigger: 'blur' }
          ],
        }
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {


  },
  // 监控data中的数据变化
  watch: {


 },
  methods: {
       canCleFn(){
           this.$router.go(-1)
       },
       submitForm() {
           this.$refs["form"].validate((valid) => {
            if (valid) {
                    this.addshopsreasApi()
            } else {
              console.log('error submit!!');
              return false;
            }
        });
       },
       async addshopsreasApi(){
         let res=""
         if(this.disabledFlag){  //修改
              this.ruleForm.id=this.rowData.id
              res= await Api.editshopsreasApi(this.ruleForm)
           }else{  //新增
              res= await Api.addshopsreasApi(this.ruleForm)
           }
            if(res.IsPositive){
              this.$message.success({message:"操作成功"})
              this.$router.push({path:'/platform/shop/region'})
            }else{
              this.$message.error({message:res.ErrorCode})
            }
       }
  },
  async created() {
        this.getselect = await this.$getDeviceType(1,true)
        if(Object.keys(this.$route.query).length>0){              //ES6写法, Object.keys(targetObject)  将对象的key作为一个数组返回
                  this.disabledFlag=true        //true代表修改数据
                  this.rowData=this.$route.query.data
                  this.ruleForm={
                           no:this.rowData.no, 
                           name:this.rowData.name, 
                           cityid:this.rowData.cityid, 
                           address:this.rowData.address, 
                           coveragearea:this.rowData.coveragearea,
                           principal:this.rowData.principal,
                           phone:this.rowData.phone
                  }
        }
  },
  mounted() {


  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='less'>


</style>